#include "StdAfx.h"
#include "main_frame.h"
#include "const_common_controlnames.h"

DesktopFrame::DesktopFrame() : bk_image_index_(2)
{
}

DesktopFrame::~DesktopFrame(void)
{
	PostQuitMessage(0);
}

CControlUI* DesktopFrame::CreateControl(LPCTSTR pstrClass)
{
	TCHAR str[1024];
	_stprintf_s(str, 1023, _T("MainFrame::CreateControl pstrClass [%s]\n"), pstrClass);
	OutputDebugString(str);
	return NULL;
}

void DesktopFrame::OnFinalMessage(HWND hWnd)
{
	WindowImplBase::OnFinalMessage(hWnd);
	delete this;
}

LPCTSTR DesktopFrame::GetWindowClassName() const
{
	return _T("TXGuiFoundation");
}

CDuiString DesktopFrame::GetSkinFile()
{
	return _T("main_frame.xml");
}

CDuiString DesktopFrame::GetSkinFolder()
{
	return _T("skin\\");
}

LRESULT DesktopFrame::ResponseDefaultKeyEvent(WPARAM wParam)
{
	switch (wParam)
	{
	case VK_ESCAPE:
		return TRUE;
	default:
		return FALSE;
	}
}

DWORD DesktopFrame::GetBkColor()
{
	CControlUI* background = m_PaintManager.FindControl(kBackgroundControlName);
	return background ? background->GetBkColor() : 0;
}

void DesktopFrame::SetBkColor(DWORD dwBkcolor)
{
	CControlUI* background = m_PaintManager.FindControl(kBackgroundControlName);
	if (background != NULL)
	{
		background->SetBkImage(_T(""));
		background->SetBkColor(dwBkcolor);
		background->NeedUpdate();

		SkinChangedParam param;
		param.bkcolor = background->GetBkColor();
		param.bgimage = background->GetBkImage();
		skin_changed_observer_.Broadcast(param);
	}
}

void DesktopFrame::Notify(TNotifyUI &msg)
{
	if (_tcsicmp(msg.sType, _T("windowinit")) == 0)
		OnPrepare(msg);
	else if (_tcsicmp(msg.sType, _T("killfocus")) == 0)
	{

	}
	else if (_tcsicmp(msg.sType, _T("click")) == 0)
	{
		DuiLib::CDuiString controlname = msg.pSender->GetName();
		if (_tcsicmp(controlname, kCloseButtonControlName) == 0)
			Close();
		else if (_tcsicmp(controlname, kMinButtonControlName) == 0)
			SendMessage(WM_SYSCOMMAND, SC_MINIMIZE, 0);
		else if (_tcsicmp(msg.pSender->GetName(), kChangeBkSkinControlName) == 0)
		{
			CControlUI* background = m_PaintManager.FindControl(kBackgroundControlName);
			if (background != NULL)
			{
				TCHAR szBuf[MAX_PATH] = {0};
				++bk_image_index_;
				if (kBackgroundSkinImageCount <= bk_image_index_)
					bk_image_index_ = 0;

				_stprintf_s(szBuf, MAX_PATH - 1, _T("file='bg%d.png' corner='600,200,1,1'"), bk_image_index_);
				background->SetBkImage(szBuf);

				SkinChangedParam param;
				CControlUI* background = m_PaintManager.FindControl(kBackgroundControlName);
				if (background != NULL)
				{
					param.bkcolor = background->GetBkColor();
					if (_tcslen(background->GetBkImage()) > 0)
						_stprintf_s(szBuf, MAX_PATH - 1, _T("bg%d.png"), bk_image_index_);

					param.bgimage = szBuf;
				}
				skin_changed_observer_.Broadcast(param);
			}
		}
	}
// 	TCHAR str[1024];
// 	_stprintf_s(str, 1023, _T("MainFrame::Notify msg.sType [%s] [%s] [%s]\n"), 
// 		msg.sType, msg.pSender ? msg.pSender->GetName() : _T("NULL"));
// 	OutputDebugString(str);
}

void DesktopFrame::OnPrepare(TNotifyUI& msg)
{
	CControlUI* bg = m_PaintManager.FindControl(kBackgroundControlName);
	if (bg != NULL)
	{
		TCHAR szBuf[MAX_PATH] = {0};
		_stprintf_s(szBuf, MAX_PATH - 1, _T("file='bg%d.png' corner='600,200,1,1'"), bk_image_index_);
		bg->SetBkImage(szBuf);
	}
}

LRESULT DesktopFrame::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	POINT pt; pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam);
	::ScreenToClient(*this, &pt);

	RECT rcClient;
	::GetClientRect(*this, &rcClient);

	if( !::IsZoomed(*this) ) {
		RECT rcSizeBox = m_PaintManager.GetSizeBox();
		if( pt.y < rcClient.top + rcSizeBox.top ) {
			if( pt.x < rcClient.left + rcSizeBox.left ) return HTTOPLEFT;
			if( pt.x > rcClient.right - rcSizeBox.right ) return HTTOPRIGHT;
			return HTTOP;
		}
		else if( pt.y > rcClient.bottom - rcSizeBox.bottom ) {
			if( pt.x < rcClient.left + rcSizeBox.left ) return HTBOTTOMLEFT;
			if( pt.x > rcClient.right - rcSizeBox.right ) return HTBOTTOMRIGHT;
			return HTBOTTOM;
		}
		if( pt.x < rcClient.left + rcSizeBox.left ) return HTLEFT;
		if( pt.x > rcClient.right - rcSizeBox.right ) return HTRIGHT;
	}

	CControlUI* pControl = static_cast<CControlUI*>(m_PaintManager.FindControl(pt));
	// 			TCHAR str[MAX_PATH];
	// 			_stprintf_s(str, MAX_PATH - 1, _T("MainFrame::OnNcHitTest GetClass %s\n"), pControl->GetClass());
	// 			OutputDebugString(str);
	if (pControl && _tcsicmp(pControl->GetClass(), _T("LayoutUI")) > 0)
		return HTCAPTION;

	return HTCLIENT;
}

LRESULT DesktopFrame::OnSysCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	bool pre_iszoomed = ::IsZoomed(m_hWnd);
	CWindowWnd::HandleMessage(uMsg, wParam, lParam);
	switch (wParam)
	{
	case SC_CLOSE:
		Close();
		break;

// 	case SC_MAXIMIZE:
// 		{
// 			CControlUI* pControl = m_PaintManager.FindControl(kMaxButtonControlName);
// 			if (pControl) pControl->SetVisible(false);
// 			pControl = m_PaintManager.FindControl(kRestoreButtonControlName);
// 			if (pControl) pControl->SetVisible(true);
// 		}
// 		break;
// 
// 	case SC_RESTORE:
// 		{
// 			CControlUI* pControl = m_PaintManager.FindControl(kMaxButtonControlName);
// 			if (pControl) pControl->SetVisible(true);
// 			pControl = m_PaintManager.FindControl(kRestoreButtonControlName);
// 			if (pControl) pControl->SetVisible(false);
// 		}
// 		break;
	}

	bool iszoomed = ::IsZoomed(m_hWnd);

	if (pre_iszoomed != iszoomed)
	{
		CControlUI* pControl = m_PaintManager.FindControl(kMaxButtonControlName);
		if (pControl) pControl->SetVisible(pre_iszoomed);
		pControl = m_PaintManager.FindControl(kRestoreButtonControlName);
		if (pControl) pControl->SetVisible(iszoomed);
	}


	return 0;
}